#!/usr/bin/python
# -*- coding: UTF-8 -*-

import pygame
import random
import math

# 初始化Pygame
pygame.init()

# 游戏窗口设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("坦克大战")
clock = pygame.time.Clock()

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
SILVER = (192, 192, 192)
BROWN = (139, 69, 19)

# 游戏参数
TANK_SIZE = 40
BULLET_SPEED = 5
WALL_SIZE = 20


class Tank:
    def __init__(self, x, y, color, is_player=True):
        self.rect = pygame.Rect(x, y, TANK_SIZE, TANK_SIZE)
        self.color = color
        self.direction = 'up'
        self.speed = 2
        self.health = 100
        self.max_health = 100
        self.bullets = []
        self.level = 1
        self.is_player = is_player

    def draw(self):
        pygame.draw.rect(screen, self.color, self.rect)
        # 绘制炮管
        if self.direction == 'up':
            end_pos = (self.rect.centerx, self.rect.top - 10)
        elif self.direction == 'down':
            end_pos = (self.rect.centerx, self.rect.bottom + 10)
        elif self.direction == 'left':
            end_pos = (self.rect.left - 10, self.rect.centery)
        else:
            end_pos = (self.rect.right + 10, self.rect.centery)
        pygame.draw.line(screen, BLACK, self.rect.center, end_pos, 5)

        # 绘制血条
        health_width = self.rect.width * (self.health / self.max_health)
        pygame.draw.rect(screen, RED, (self.rect.left, self.rect.top - 10, self.rect.width, 5))
        pygame.draw.rect(screen, GREEN, (self.rect.left, self.rect.top - 10, health_width, 5))

        # 绘制等级
        font = pygame.font.Font(None, 20)
        text = font.render(f"Lv:{self.level}", True, BLACK)
        screen.blit(text, (self.rect.left, self.rect.top - 25))

    def move(self, direction):
        self.direction = direction
        dx, dy = 0, 0
        speed = self.speed

        if direction == 'up':
            dy = -speed
        elif direction == 'down':
            dy = speed
        elif direction == 'left':
            dx = -speed
        elif direction == 'right':
            dx = speed

        # 碰撞检测
        new_rect = self.rect.move(dx, dy)
        if 0 <= new_rect.left and new_rect.right <= WIDTH and 0 <= new_rect.top and new_rect.bottom <= HEIGHT:
            collision = False
            for wall in walls:
                if new_rect.colliderect(wall.rect):
                    collision = True
                    break
            if not collision:
                self.rect = new_rect

    def fire(self):
        if len(self.bullets) < 3 + self.level // 2:  # 子弹数量随等级增加
            x, y = self.rect.center
            if self.direction == 'up':
                bullet = Bullet(x, y - 20, 0, -BULLET_SPEED - self.level // 2)
            elif self.direction == 'down':
                bullet = Bullet(x, y + 20, 0, BULLET_SPEED + self.level // 2)
            elif self.direction == 'left':
                bullet = Bullet(x - 20, y, -BULLET_SPEED - self.level // 2, 0)
            else:
                bullet = Bullet(x + 20, y, BULLET_SPEED + self.level // 2, 0)
            self.bullets.append(bullet)


class Bullet:
    def __init__(self, x, y, dx, dy):
        self.rect = pygame.Rect(x - 3, y - 3, 6, 6)
        self.dx = dx
        self.dy = dy

    def move(self):
        self.rect.x += self.dx
        self.rect.y += self.dy


class Wall:
    def __init__(self, x, y, wall_type):
        self.rect = pygame.Rect(x, y, WALL_SIZE, WALL_SIZE)
        self.type = wall_type
        self.health = 3 if wall_type == 'earth' else 1000

    def draw(self):
        color = BROWN if self.type == 'earth' else SILVER
        pygame.draw.rect(screen, color, self.rect)


class PowerUp:
    def __init__(self):
        self.rect = pygame.Rect(random.randint(0, WIDTH - 20), random.randint(0, HEIGHT - 20), 20, 20)
        self.type = 'star'

    def draw(self):
        pygame.draw.circle(screen, YELLOW, self.rect.center, 10)


# 生成随机地图
walls = []
for i in range(0, WIDTH, WALL_SIZE):
    for j in range(0, HEIGHT, WALL_SIZE):
        if random.random() < 0.2:  # 20%概率生成墙体
            wall_type = 'earth' if random.random() < 0.7 else 'metal'
            walls.append(Wall(i, j, wall_type))

# 创建玩家坦克
player = Tank(WIDTH // 2, HEIGHT - 50, GREEN)

# 创建敌方坦克
enemies = [Tank(100, 100, RED, False), Tank(700, 100, RED, False)]

# 游戏循环
powerup = None
last_powerup_time = pygame.time.get_ticks()
running = True
while running:
    # 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 玩家控制
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
        player.move('up')
    if keys[pygame.K_s]:
        player.move('down')
    if keys[pygame.K_a]:
        player.move('left')
    if keys[pygame.K_d]:
        player.move('right')
    if keys[pygame.K_SPACE]:
        player.fire()

    # 生成道具
    current_time = pygame.time.get_ticks()
    if current_time - last_powerup_time > 10000:  # 10秒生成道具
        powerup = PowerUp()
        last_powerup_time = current_time

    # 碰撞检测
    # 子弹碰撞检测
    for tank in [player] + enemies:
        for bullet in tank.bullets[:]:
            bullet.move()
            # 边界检测
            if not (0 <= bullet.rect.centerx <= WIDTH and 0 <= bullet.rect.centery <= HEIGHT):
                tank.bullets.remove(bullet)
                continue

            # 墙体碰撞
            for wall in walls[:]:
                if bullet.rect.colliderect(wall.rect):
                    wall.health -= 1
                    if wall.health <= 0:
                        walls.remove(wall)
                    tank.bullets.remove(bullet)
                    break

            # 坦克碰撞
            for target in [player] + enemies:
                if target != tank and bullet.rect.colliderect(target.rect):
                    target.health -= 10
                    if target.health <= 0:
                        if target in enemies:
                            enemies.remove(target)
                    tank.bullets.remove(bullet)
                    break

    # 道具拾取
    if powerup and player.rect.colliderect(powerup.rect):
        player.level = min(player.level + 1, 10)
        powerup = None

    # 绘制
    screen.fill(WHITE)

    # 绘制墙体
    for wall in walls:
        wall.draw()

    # 绘制道具
    if powerup:
        powerup.draw()

    # 绘制坦克
    player.draw()
    for enemy in enemies:
        enemy.draw()

    # 绘制子弹
    for tank in [player] + enemies:
        for bullet in tank.bullets:
            pygame.draw.rect(screen, BLACK, bullet.rect)

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

